/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.qudexapps.swfspreadsheet.actions;

//import java.net.FileNameMap;
//import java.net.URLConnection;
import java.util.List;

import org.springframework.binding.message.MessageBuilder;
//import org.springframework.web.multipart.MultipartFile;
import org.springframework.webflow.action.AbstractAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

//import uk.ljmu.qudexapps.swfspreadsheet.domain.Files;
import uk.ljmu.qudexapps.swfspreadsheet.domain.MdSchemaInstance;

/**
 * POJO Action that validates that the USER inputs needed before generate the
 * spreadsheet are correct, e.g. He has selected at least one metadata schema
 * element, or if he has provided a ZIP file, check this is in the right format
 * 
 * @author agustinamartinez
 * 
 */
public class CheckSpreadsheetAction extends AbstractAction {

    @SuppressWarnings("unchecked")
    @Override
    protected Event doExecute(RequestContext context) throws Exception {
	List<MdSchemaInstance> schemas;

	// AMG - Get DiskFileItem Instance managing the uploaded ZIP file
	//Files filesObj = (Files) context.getFlowScope().get("files");
	// AMG - Check that at list one element from a metadata schema has been
	// selected OR one data header
	if (context.getFlowScope().get("schemasSelected") instanceof List<?>) {
	    // AMG - Get data from the metadata array and the data headers array
	    schemas = (List<MdSchemaInstance>) context.getFlowScope().get(
		    "schemasSelected");

	    // AMG - no elements selected return validation error
	    boolean allDeleted = true;
	    for (MdSchemaInstance instance : schemas) {
		if (!instance.isRemoved()) {
		    allDeleted = false;
		    break;
		}
		    
	    }
	    //if (schemas.size() == 0) {
	    if (allDeleted) {
		context.getMessageContext().addMessage(
			new MessageBuilder().error().code("emptyArrays")
				.build());
		return error();
	    } else {
		// AMG - Check whether a ZIP file has been provided
		/*if (filesObj.getZipFiles() != null
			&& filesObj.getZipFiles().size() > 0) {
		    // Check mimetype
		    FileNameMap fileNameMap = URLConnection.getFileNameMap();
		    for(MultipartFile fileZip : filesObj.getZipFiles()) {
			String mimeType = fileNameMap.getContentTypeFor(fileZip.getOriginalFilename());
		    	if (mimeType == null
			    || (!mimeType.equalsIgnoreCase("application/zip")
				    && !mimeType
					    .equalsIgnoreCase("application/x-compress")
				    && !mimeType
					    .equalsIgnoreCase("application/x-gzip")
				    && !mimeType
					    .equalsIgnoreCase("application/x-gtar") && !mimeType
				    .equalsIgnoreCase("application/x-tar"))) {
				context.getMessageContext().addMessage(
				new MessageBuilder().error().code(
					"invalidZipFileFormat").build());
				return error();
		    	}
		    }
		}*/
	    }
	}

	return success();
    }
}
